package com.drojian.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {
    private static final String TAG = "MyContentProvider";
    private static final String AUTHORITIES = "com.drojian.provider.provider";
    private static final int BOOK_URI_CODE = 0;
    private static final int USER_URI_CODE = 1;
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private SQLiteDatabase mDB;
    private Context mContext;
    public MyContentProvider() {
    }
    static {
        sUriMatcher.addURI(AUTHORITIES,"book",BOOK_URI_CODE);
        sUriMatcher.addURI(AUTHORITIES,"user",USER_URI_CODE);
    }

    @Override
    public boolean onCreate() {
        mContext = getContext();
        mDB = new DB(mContext, "user.db", null, 1).getWritableDatabase();
        mDB.execSQL("delete from book_table");
        mDB.execSQL("delete from user_table");
        mDB.execSQL("insert into book_table values (1,'android 开发');");
        mDB.execSQL("insert into book_table values (2,'java');");
        mDB.execSQL("insert into book_table values (3,'ios');");
        mDB.execSQL("insert into user_table values (1,'张三',18);");
        mDB.execSQL("insert into user_table values (2,'李四',20);");
        return true;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        String name = getTableName(uri);
        return  mDB.query(name,projection,selection,selectionArgs,
                null,null,sortOrder,null);
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        String name = getTableName(uri);
        mDB.insert(name,null,values);
        mContext.getContentResolver().notifyChange(uri,null);
        return uri;
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        String name = getTableName(uri);
        int count = mDB.delete(name, selection, selectionArgs);
        if(count >0){
            getContext().getContentResolver().notifyChange(uri,null);
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        String name = getTableName(uri);
        int row = mDB.update(name, values, selection, selectionArgs);
        if(row>0){
            getContext().getContentResolver().notifyChange(uri,null);
        }
        return row;
    }
    private String getTableName(Uri uri){
        String tableName = null;
        switch (sUriMatcher.match(uri)){
            case BOOK_URI_CODE:
                tableName = "book_table";
                break;
            case USER_URI_CODE:
                tableName = "user_table";
                break;
        }
        return tableName;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}